import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
import { OrderService } from 'src/app/services/order.service';
import * as constants from '../../../providers/Constants'
import { ToastService } from 'src/app/providers/toast/toast.service';
import { ShopService } from 'src/app/services/shop.service';
import { AlertController } from '@ionic/angular';
import { Utils } from 'src/app/providers/Utils';

@Component({
  selector: 'app-order-list',
  templateUrl: './order-list.page.html',
  styleUrls: ['./order-list.page.scss'],
})
export class OrderListPage implements OnInit {
  menuIndex: number = 1;
  nowPage: number = 1;
  pageSize: number = 20;
  procOrderStatus: string = '';
  goProcOrderStatus: string = '';
  imageUrl: string = '';
  orderList: any = [];
  menuList: any = [
    // { id: '0', value: '待付款', select: true },
    { id: '2', value: 'Ordered', select: false },
    { id: '4', value: 'Shipped', select: false },
    { id: '0', value: 'Delivered', select: false },
    { id: '10', value: 'All Orders', select: false },
  ];//菜单列表
  constructor(private nav: NavController, private activeRoute: ActivatedRoute, public navCtrl: NavController, private shop: ShopService, private alertController: AlertController, private mOrderService: OrderService, private toast: ToastService) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      if (params.orderType != undefined) {
        console.log('params.orderType==', params.orderType);
        if(this.goProcOrderStatus==''){
          this.menuIndex = params.orderType;
          this.goProcOrderStatus=params.orderType;
          for (let index = 0; index < this.menuList.length; index++) {
            const element = this.menuList[index];
            if (element.id == this.menuIndex) {
              this.menuList[index].select = true;
            } else {
              this.menuList[index].select = false;
            }
          }
          this.procOrderStatus = params.orderType
        }else{
          this.procOrderStatus=this.goProcOrderStatus;
          this.query();
        }
        this.imageUrl = this.mOrderService.getImageUrl();
      }
    console.log('返回的  this.procOrderStatus==',  this.procOrderStatus);
    });

    
  }
  onComment(item) {
    console.log(item);
    console.log('去之前  this.procOrderStatus==',  this.procOrderStatus);
    this.goProcOrderStatus=this.procOrderStatus;
    this.nav.navigateForward('order-comment', {
      queryParams: {
        commentInfo: JSON.stringify(item),
      }
    });
  }
  strFormatData(str) {
    return Utils.strFormatData(str);
  }
  btnOK(item) {
    item.array.forEach((element,index) => {
      this.mOrderService.markSureProcOrder({
        orderId: element.procOrderId
      }).subscribe(res => {
        if (res.retCode == '000000') {
          if(index+1==item.array.length){
            this.query();
          }
        } else if (constants.errCode[res.retCode]) {
          if (constants.errCode[res.retCode] != '0') { this.toast.show(constants.errCode[res.retCode]); }
        } else if (res.retMsg) {
          this.toast.show(res.retMsg);
        } else {
          this.toast.show("exception service routine");
        }
      })
    });

}
logistics(item){
  this.goProcOrderStatus=this.procOrderStatus;
  this.nav.navigateForward('logistics', {
    queryParams: {
      commentInfo: JSON.stringify(item),
      procOrderId: item.array[0].procOrderId,
    }
  });
}
btn(){
  this.toast.show('The development of');
}

onClickItem(item) {
  this.nav.navigateForward(['/goods-details'], {
    queryParams: {
      shopInfo: JSON.stringify(item),
    }
  });
}

segmentChanged(event) {
  console.log(event);
  this.menuList.forEach((element, index) => {
    console.log(event.detail.value + '===', element.id);
    if (event.detail.value == element.id) {
      this.menuList[index].select = true;
    } else {
      this.menuList[index].select = false;
    }
  });
  this.procOrderStatus = event.detail.value;
  this.nowPage = 1;
  this.orderList = [];
  this.query();
  console.log(this.menuList);
}
ngOnInit() {
  this.query();
  
}











query() {
  console.log('加载了-----------------------------');
  this.mOrderService.queryProcOrder({
    nowPage: this.nowPage,
    pageSize: this.pageSize,
    procOrderStatus: this.procOrderStatus,
  }).subscribe(res => {
    if (res.data) {
      if (res.retCode == '000000') {
        // res={"retCode":"000000","retMsg":"调用成功","data":[{"orderDate":"20200109","procOrderStatus":"0","orderId":"O0202001090000003679","orderAmt":"17","array":[{"tranTime":"10:41:40","procOrderId":"PO0202001090000002043","num":"1","tranDate":"20200122","procImg":"002c2500-f0df-495c-99fe-893aaa248246","procId":"PROC202001070000000082","procPriceId":"PR202001070000000023","color":"雅黑","procName":"索尼智能电视","procOrderStatus":"3","procAmt":"40000","procOrderType":"0","procCashPrice":"40000","size":"32"},{"tranTime":"17:19:09","procOrderId":"PO0202001090000002044","num":"2","tranDate":"20200121","procImg":"f06b9edd-e4f6-4410-83c7-79652073c9c7","procId":"PROC202001070000000080","procPriceId":"PR202001070000000022","color":"暗红","procName":"羽绒服男冬季","procOrderStatus":"0","procAmt":"100000","procOrderType":"0","procCashPrice":"50000","size":"175"}]}]}
        res.data.forEach(element => {
          // element.tranDate = Utils.strFormatData2(element.tranDate );
          for (let index = 0; index < element.array.length; index++) {
            const element1 = element.array[index];
            element1.procImg = element1.procImg.split(',')[0]
          }
          if (element.procOrderStatus == '0') {
            element.procOrderStatusStr = "Done"
          } else if (element.procOrderStatus == '1') {
            element.procOrderStatusStr = "defeated"
          } else if (element.procOrderStatus == '2') {
            element.procOrderStatusStr = "Ordered"
          } else if (element.procOrderStatus == '3') {
            element.procOrderStatusStr = ""
          } else if (element.procOrderStatus == '4') {
            element.procOrderStatusStr = "Shipped"
          } else if (element.procOrderStatus == '5') {
            element.procOrderStatusStr = "Done"
          } else if (element.procOrderStatus == '6') {
            element.procOrderStatusStr = "cancel"
          } else {
            element.procOrderStatusStr = ""
          }
        });
        this.orderList = res.data;
        console.log('orderList=',this.orderList);
        
        // this.infiniteScroll.disabled = true;

      } else if (constants.errCode[res.retCode]) {
        if (constants.errCode[res.retCode] != '0') { this.toast.show(constants.errCode[res.retCode]); }
      } else if (res.retMsg) {
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }

    }
    console.log('All Orders返回的', res);

  })

}

// segmentChanged(event) {
//   this.menuList.forEach((element, index) => {
//     console.log(event.detail.value + '===', element.id);
//     if (event.detail.value == element.id) {
//       this.menuList[index].select = true;
//     } else {
//       this.menuList[index].select = false;
//     }
//   });

// }
AmountOfConversion(num: number) {
  return num / 100;
}

}


